package com.epoint.BookManager.action;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import com.epoint.BookManager.domain.BookInfo;
import com.epoint.BookManager.service.BookInfoService;
import com.epoint.BookManager.utils.StringUtil;

@WebServlet("/BookInfoListAction")
public class BookInfoListAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
    BookInfoService bookInfoService = new BookInfoService();
    /**	
     * @see HttpServlet#HttpServlet()
     */
    public BookInfoListAction() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String method = request.getParameter("method");
		if("getbookinfolist".equals(method)) {
			try {
				getBookInfoList(request, response);
			} catch (NumberFormatException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public void getBookInfoList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, NumberFormatException, SQLException {
		String pageIndex = request.getParameter("pageIndex");
		int intBookType = 0;
		//页面大小
		String pageSize = request.getParameter("pageSize");
		//书籍类型 	
		String bookType = request.getParameter("booktype");
		if (StringUtil.isNotBlank(bookType)){
			 intBookType = Integer.parseInt(bookType);
		}
		String sortField = request.getParameter("sortField");
		//排序方式 	 	
		String sortOrder = request.getParameter("sortOrder");
		//书籍名字
		String bookname = request.getParameter("bookname");

		List<BookInfo> list = bookInfoService.getBookList(bookname, intBookType, Integer.parseInt(pageIndex), Integer.parseInt(pageSize), sortField, sortOrder);
		Map<String ,Object> result = new HashMap<>();
		result.put("data",list );
		result.put("total",bookInfoService.getBookList(bookname, 2, 0, Integer.MAX_VALUE, sortField, sortOrder).size());
		response.getWriter().print(JSONObject.toJSONStringWithDateFormat(result,  "YYYY-MM-dd"));
	}
	
}
